import java.net.*;
import java.io.*;

public class Server
{
	public static void main(String args[])
	{
		ServerSocket serverSocket = null;
		Socket socket = null;
		InputStream inp = null;
		BufferedReader brinp = null;
		DataOutputStream out = null;
		try{
			serverSocket = new ServerSocket(6666);
		}
		catch(IOException e){
			System.out.println("Bd przy tworzeniu gniazda serwerowego.");
			System.exit(-1);
		}
		System.out.println("Inicjalizacja gniazda zakoczona...");
		System.out.println("Parametry gniazda: " + serverSocket);
		try{
			socket = serverSocket.accept();
		}
		catch(IOException e){
			System.out.println(e);
		}
		System.out.println("Nadeszo poczenie...");
		System.out.println("Parametry poczenia: " + socket);
		try{
			inp = socket.getInputStream();
			brinp = new BufferedReader(new InputStreamReader(inp));
			out = new DataOutputStream(socket.getOutputStream());
		}
		catch(IOException e){
			System.out.println("Bd przy tworzeniu strumieni.");
			System.exit(-1);
		}
		System.out.println("Zakoczona inicjalizacja strumieni...");
		String line;
		System.out.println("Rozpoczcie ptli gwnej...");
		while(true){
			try{
				line = brinp.readLine();
				System.out.println("Odczytano lini: " + line);
				if((line == null) || line.equals("quit")){
					try{
						serverSocket.close();
					}
					catch(IOException e){
			System.out.println("Bd przy zamykaniu gniazda serwerowego");
					}
					System.out.println("Zakoczenie pracy...");
					System.exit(0);
				}
				out.writeBytes(line + "\n\r");
				System.out.println("Wysano lini: " + line);
			}
			catch(IOException e){
				System.out.println("Bd wejcia-wyjcia.");
				System.exit(-1);
			}
		}
	}
}
